package controller.servletCadastro;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.classDAO.ClienteDAO;
import model.classDAO.PeriodoDAO;
import model.classDAO.TipoServicoDAO;
import model.classes.Cliente;
import model.classes.Periodo;
import model.classes.Servico;
import model.classes.TipoServico;
import model.factory.FactoryDAO;

public class ServletCadastroServico extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5492813010931449448L;
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
	{
		RequestDispatcher rd = null;
		String nome = request.getParameter("nomeservico");
		String descricao = request.getParameter("descricaoservico");
		String descricaoPeriodo = request.getParameter("descricaoperiodo");
		String duracao = request.getParameter("duracaoperiodo");
		
		
		long ace = 0;
 		if(duracao != null || !duracao.equals("") || duracao.equals("0"))
 			ace = Long.parseLong(duracao);
 		
		TipoServico servico = new TipoServico();
		Periodo periodo = new Periodo();
		
		servico.setNome(nome);
		servico.setDescricao(descricao);
		periodo.setDescricao(descricaoPeriodo);
		periodo.setDuracao(ace);
		
		TipoServicoDAO tsDAO = FactoryDAO.getTipoServicoDAO();
		tsDAO.cadastrar(servico);
		
		PeriodoDAO pDAO = FactoryDAO.getPeriodoDAO();
		pDAO.cadastrar(periodo);
		
		rd = request.getRequestDispatcher("/index.jsp");
		try {
			rd.forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
